package com.chatai.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestAIController {

    @PostMapping("/api/test-ai")
    public ResponseEntity<?> mockAIResponse(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        List<?> messages = (List<?>) request.get("messages");
        
        Map<String, String> content = new HashMap<>();
        if (messages != null && !messages.isEmpty()) {
            Map<?, ?> lastMessage = (Map<?, ?>) messages.get(messages.size() - 1);
            String userMessage = (String) lastMessage.get("content");
            content.put("content", "这是对\"" + userMessage + "\"的模拟AI回复。在实际部署中，这里会连接到真实的AI服务。");
        } else {
            content.put("content", "这是一条模拟的AI回复。");
        }
        
        content.put("role", "assistant");
        
        Map<String, Object> choice = new HashMap<>();
        choice.put("message", content);
        
        response.put("choices", List.of(choice));
        
        return ResponseEntity.ok(response);
    }
}